home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9110.ZIP / STRING.ZIP / STRIIO.CPP < prev    next >
C/C++ Source or Header  |  1991-07-15  |  1KB  |  52 lines

  1. #include <ctype.h>
  2. #include <iostream.hpp>
  3. #include <string.hpp>
  4.  
  5. istream &operator>>(istream &is, String &s)
  6. {
  7.     
  8.     char buf[81];
  9.     if (is.ipfx(0)) {
  10.         for (int n = 0;;) {
  11.             int c = is.rdbuf()->sgetc();
  12.             if (c == EOF) {
  13.                 is.clear(ios::badbit | ios::eofbit | is.rdstate());
  14.                 break;
  15.             }
  16.             if (isspace(c)) break;
  17.             is.rdbuf()->stossc();
  18.             buf[n++] = c;
  19.         }
  20.         buf[n] = '\0';
  21.     }
  22.     s.operator=(buf);
  23.     return is;
  24. }
  25.  
  26. ostream &operator<<(ostream &os, const String &s)
  27. {
  28.     if (os.opfx()) {
  29.         int n;
  30.         if (os.flags() & (ios::left | ios::internal)) {
  31.             for (int n = os.width()-s.length(); n > 0; --n) {
  32.                 os.put('.');
  33.                 if (!os.good()) goto done;
  34.             }
  35.         }
  36.         n = s.length();
  37.         for (int i = 0; i < n; ++i) {
  38.             os.put(*(s.body()+i));
  39.             if (!os.good()) goto done;
  40.         }
  41.         if (os.flags() & (ios::right)) {
  42.             for (n = os.width()-s.length(); n > 0; --n) {
  43.                 os.put('.');
  44.                 if (!os.good()) goto done;
  45.             }
  46.         }
  47.     }
  48.     done:
  49.     os.osfx();
  50.     return os;
  51. }
  52.